//
//  SheetView.m
//  DriveStudy
//
//  Created by chen on 2017/4/20.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "SheetView.h"

@interface SheetView(){
    
    //是否可以滑动
    BOOL _startMoving;
    float _width;
    float _hight;
    float _originY;
    
    UIScrollView *_scrollView;
    //传入的题目值
    int _count;
}
@end
@implementation SheetView

-(instancetype)initWithFrame:(CGRect)frame withSuperView:(UIView *)view andQuestion:(int)count{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        _superView = view;
        _width = frame.size.width;
        _hight = frame.size.height;
        _originY = frame.origin.y;
        _count = count;
        [self createUI];
    }
    return self;
}
-(void)createUI{
    _backView = [[UIView alloc]initWithFrame:_superView.frame];
    _backView.backgroundColor = [UIColor grayColor];
    _backView.alpha = 0.0;
    [_superView addSubview:_backView];
    //添加答题的题目button
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, self.frame.size.width, self.frame.size.height - 70)];
    
    [self addSubview:_scrollView];
    for (int i =0; i<_count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        //button与button之间是4个像素，一排是6个button放着，_width - 44 * 6 /2 是前后的间距
        btn.frame = CGRectMake((_width - 44*6)/2 + 44 * (i%6), 10 + 44*(i/6), 40, 40);
        btn.backgroundColor = [UIColor colorWithRed:220/250.0 green:220/250.0 blue:220/250.0 alpha:1];
        btn.tag = 300+i;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        //刚开始点进来时第一个应该是橘黄色
        if (i == 0) {
            btn.backgroundColor = [UIColor orangeColor];
        }
        [btn setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 8;
        [_scrollView addSubview:btn];
    }
    int tip = (_count % 6)?1:0;
    _scrollView.contentSize = CGSizeMake(0, 20 + 44*(_count/6 +1 + tip));
}
-(void)click:(UIButton *)button{
    int index = (int)button.tag;
    button.backgroundColor = [UIColor orangeColor];
    //点击一次button以后button背景变换色，那么需要从头遍历一遍，将其他的button背景色给改变过去
    for (int i =0; i<_count -1 ; i++) {
        UIButton *btn = [self viewWithTag:i + 300];
        if (i != index - 300) {
            btn.backgroundColor = [UIColor colorWithRed:220/250.0 green:220/250.0 blue:220/250.0 alpha:1];
        }
    }
    [_delegate SheetViewClick:index - 300];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    //触点
    CGPoint point = [touch locationInView:[touch view]];
    if (point.y > 25) {
        _startMoving = YES;
    }
    if (_startMoving && self.frame.origin.y >= _originY - _hight && [self convertPoint:point toView:_superView].y >= 80) {
        self.frame = CGRectMake(0, [self convertPoint:point toView:_superView].y, _width, _hight);
        //在移动的过程中进行偏移量的改变
        //self.frame.origin.y是答案视图的位移位置
        float offset = (_superView.frame.size.height - self.frame.origin.y)/_superView.frame.size.height * 0.8;
        _backView.alpha = offset;
    }
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _startMoving = NO;
    //如果滑动结束超过了_superView的一半高度就自动收缩回去
    if (self.frame.origin.y > _originY - _hight/2) {
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, _originY, _width, _hight);
            _backView.alpha = 0.0;
        }];
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, _originY - _hight, _width, _hight);
            _backView.alpha = 0.8;
        }];
    }
}

@end
